package p07_connections;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;
import org.junit.jupiter.api.Test;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

public class C3P0 {
    /**
     * 方式一：硬编码
     * @throws PropertyVetoException
     * @throws SQLException
     */
    @Test
    public void testGetConnection1() throws PropertyVetoException, SQLException {
        ComboPooledDataSource cpds = new ComboPooledDataSource();
        cpds.setDriverClass("com.mysql.jdbc.Driver");
        cpds.setJdbcUrl("jdbc:mysql://localhost:3307/test?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true");
        cpds.setUser("root");
        cpds.setPassword("ys19931111");
        // 设置连接数
        cpds.setInitialPoolSize(10);

        Connection conn = cpds.getConnection();
        System.out.println(conn);

        // 销毁连接池
        DataSources.destroy(cpds);
    }

    /**
     * 方式二：配置文件
     */
    @Test
    public void testGetConnection2() throws SQLException {
        ComboPooledDataSource cpds = new ComboPooledDataSource("intergalactoApp");
        Connection conn = cpds.getConnection();
    }
}
